package com.wwj.auction.login.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.login.entity.GetOut;
import com.wwj.auction.login.entity.Shop;
import com.wwj.auction.login.entity.UserInfo;
import com.wwj.auction.login.mapper.GetOutMapper;
import com.wwj.auction.login.mapper.ShopMapper;
import com.wwj.auction.login.mapper.UserInfoMapper;
import com.wwj.auction.login.service.GetOutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2022-01-02
 */
@Service
public class GetOutServiceImpl extends ServiceImpl<GetOutMapper, GetOut> implements GetOutService {

    @Resource
    private ShopMapper shopMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public void publish(Integer shopId, Integer userId, String memo, String score) {
        // 查询商品所属店铺
        Shop shop = shopMapper.selectById(shopId);
        Integer storeId = shop.getStoreId();
        String shopName = shop.getShopName();
        BigDecimal shopCurrentPrice = shop.getShopCurrentPrice();
        // 根据店铺所属人id查询店铺状态
        Integer sellerId = shop.getSellerId();
        UserInfo userInfo = userInfoMapper.selectOne(
                Wrappers.lambdaQuery(UserInfo.class)
                        .eq(UserInfo::getId, sellerId));
        Integer storeStatus = userInfo.getStoreStatus();
        // 封装商品评价
        GetOut getOut = new GetOut();
        getOut.setShopName(shopName);
        getOut.setShopPrice(shopCurrentPrice);
        getOut.setScore(Float.valueOf(score));
        getOut.setStoreStatus(storeStatus);
        getOut.setStoreId(storeId);
        getOut.setMemo(memo);
        getOut.setCreateTime(LocalDateTime.now());
        getOut.setUserId(userId);
        getOut.setShopId(shopId);

        // 保存
        baseMapper.insert(getOut);
    }

    @Override
    public GetOut checkPublish(Integer shopId, Integer userId) {
        return baseMapper.selectOne(
                Wrappers.lambdaQuery(GetOut.class)
                        .eq(GetOut::getShopId, shopId)
                        .eq(GetOut::getUserId, userId));
    }
}
